var CHANCE_SPEC, x, y;

CHANCE_SPEC = 60;
x = 0;
y = 0;

function rand(min, max) {
    return Math.floor(Math.random() * (max - min + 1) - min);
}

function drawSpec(data, x, y, w) {
    var index;

    index = (x + y * w) * 4;

    data.data[index + 0] = 0;
    data.data[index + 1] = 0;
    data.data[index + 2] = 0;
    data.data[index + 3] = 30;
}

function drawPattern(canvas, ctx, data) {
    var w, h;
    w = canvas.width;
    h = canvas.height;
    while (x < w) {
        if (rand(1, 100) < CHANCE_SPEC) {
            drawSpec(data, x, y, w);
        }
        x++;
    }
    if (x === w) {
        x = 0;
        y++;
    }
    if (y === h) {
        ctx.putImageData(data, 0, 0);
        return;
    }
    drawPattern(canvas, ctx, data);
}

function main() {
    var canvas, ctx, data;

    console.log('starting1');
    canvas = document.getElementById('my-canvas');
    ctx = canvas.getContext('2d');
    data = ctx.getImageData(0, 0, canvas.width, canvas.height);
    drawPattern(canvas, ctx, data);
    currentTime = new Date(1 * 5 * 1000 - 8 * 60 * 60 * 1000);
    updateTime(currentTime);
}

function updateTime(currentTime) {
    console.log(currentTime)

    var seconds, minutes, hours, times, i;

    if (currentTime.getTime() < -28800000) {
        playMusic();
        return;
    }

    times = {
        'second': currentTime.getSeconds(),
        'minute': currentTime.getMinutes(),
        'hour': currentTime.getHours()
    };
    for (type in times) {
        if (times.hasOwnProperty(type)) {
            setTimes(type, timeToString(times[type]));
        }
    }
    newTime = new Date(currentTime.getTime() - 1000)
    setTimeout('updateTime(newTime)', 1000);
}

function timeToString(currentTime) {
    var t;
    t = currentTime.toString();
    if (t.length === 1) {
        return '0' + t;
    }
    return t;
}

function getPreviousTime(type) {
    return $('#' + type + '-top').text();
}

function setTimes(type, timeStr) {
    setTime(getPreviousTime(type + '-tens'),
        timeStr[0], type + '-tens');
    setTime(getPreviousTime(type + '-ones'),
        timeStr[1], type + '-ones');
}

function setTime(previousTime, newTime, type) {
    if (previousTime === newTime) {
        return;
    }
    setTimeout(function () {
        $('#' + type + '-top').text(newTime);
    }, 150);
    setTimeout(function () {
        $('.bottom-container',
            $('#' + type + '-bottom')).text(newTime);
    }, 365);
    animateTime(previousTime, newTime, type);
}

function animateTime(previousTime, newTime, type) {
    var top, bottom;
    top = $('#top-' + type + '-anim');
    bottom = $('#bottom-' + type + '-anim');
    $('.top-half-num', top).text(previousTime);
    $('.dropper', bottom).text(newTime);
    top.show();
    bottom.show();
    $('#top-' + type + '-anim').css('visibility', 'visible');
    $('#bottom-' + type + '-anim').css('visibility', 'visible');
    animateNumSwap(type);
    setTimeout(function () {
        hideNumSwap(type);
    }, 365);
}

function animateNumSwap(type) {
    $('#top-' + type + '-anim').toggleClass('up');
    $('#bottom-' + type + '-anim').toggleClass('down');
}

function hideNumSwap(type) {
    $('#top-' + type + '-anim').toggleClass('up');
    $('#bottom-' + type + '-anim').toggleClass('down');
    $('#top-' + type + '-anim').css('visibility', 'hidden');
    $('#bottom-' + type + '-anim').css('visibility', 'hidden');
}

function rbf() {
    var audio = document.getElementById('music1');
    audio.currentTime = 0;
    audio.play();
}

function bf() {
    var audio = document.getElementById('music1');
    if (audio !== null) {
        //检测播放是否已暂停.audio.paused 在播放器播放时返回false.
        //alert(audio.paused);
        if (audio.paused) {
            audio.play(); //audio.play();// 这个就是播放
        } else {
            audio.pause(); // 这个就是暂停
        }
    }
}


function playMusic() {
    //存放音乐列表，根据音乐文件路径，进行填写
    let musicList = ["./audio/up2.mp3"]
    var myAudio = new Audio();
    //是否进行预加载
    myAudio.preload = false;
    //是否显示隐藏按钮
    myAudio.controls = true;
    myAudio.hidden = true;
    //从音乐列表中，获取最后一个音乐（并删除）
    var src = musicList.pop();
    myAudio.src = src;
    //将最后一个音乐添加到数组的开头，这样实现循环
    musicList.unshift(src);
    //绑定音乐结束事件，当音乐播放完成后，将会触发playEndedHandler方法
    myAudio.addEventListener("ended", playEndedHandler, false);
    //播放当前音乐
    myAudio.play();
    document.getElementById("music").appendChild(myAudio);
    //将循环播放关闭，如果开启，将不能触发playEndedHandler方法，只能进行单曲循环
    myAudio.loop = false;

    function playEndedHandler() {
        src = musicList.pop();
        myAudio.src = src;
        musicList.unshift(src);
        myAudio.play();
    }
}

$(document).ready(main);


window.requestAnimFrame = (function (callback) {
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function (callback) {
            window.setTimeout(callback, 1000 / 60);
        }
})();
